#!/bin/sh

# This script builds kernel modules

if [ -z "$ARCH" ]; then
	ARCH="x86_64"
fi
if [ -z "$CMD" ]; then
	CMD=build
fi

KERN_SRC=$(realpath $(dirname $0)/..)
export CARGOFLAGS="--target $KERN_SRC/kernel/arch/$ARCH/$ARCH.json $CARGOFLAGS"

if [ ! -z "$PROFILE" ] && [ "$PROFILE" != "debug" ]; then
	CARGOFLAGS="$CARGOFLAGS --profile $PROFILE"
else
	export PROFILE="debug"
fi
export RUSTFLAGS="--extern kernel=$KERN_SRC/kernel/target/$ARCH/$PROFILE/libkernel.rlib -L $KERN_SRC/kernel/target/$ARCH/$PROFILE/deps -L $KERN_SRC/kernel/target/$PROFILE/deps $RUSTFLAGS"

cargo "$CMD" $CARGOFLAGS $@
